package com.lianbang.staff.module.main

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.utils.ResourcesUtils
import com.lianbang.staff.R
import com.lianbang.staff.databinding.ActivityMainBinding
import com.lianbang.staff.module.shop.ShopFragment
import com.lianbang.staff.vm.MainVM

/**
 * Created by maoqi on 2020/12/28.
 */
class MainActivity : BaseActivity<MainVM, ActivityMainBinding>() {

    companion object {
        const val TAB_HOME = 0
        const val TAB_SHOP = 1
        const val TAB_ORDER = 2
        const val TAB_USER = 3

        fun start(context: Context?, tab: Int) {
            context?.also {
                val starter = Intent(it, MainActivity::class.java)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                starter.putExtra("tab", tab)
                it.startActivity(starter)
            }
        }
    }

    private val mFragmentList = mutableListOf<Fragment>()
    override fun initLayout(): Int {
        return R.layout.activity_main
    }

    override fun initData(intent: Intent) {

    }

    override fun initView() {
        mFragmentList.add(HomeFragment.newInstance())
        mFragmentList.add(ShopFragment.newInstance())
        mFragmentList.add(OrderFragment.newInstance())
        mFragmentList.add(UserFragment.newInstance())
        initBottomNavigation()
    }


    override fun loadData() {

    }

    /**
     * 底部栏
     */
    private fun initBottomNavigation() {
        val titles = arrayOf("首页", "门店", "订单", "我的")
        val icons = intArrayOf(
            R.drawable.sel_home_icon,
            R.drawable.sel_shop_icon,
            R.drawable.sel_order_icon,
            R.drawable.sel_user_icon
        )
        mBinding.bvBottom.setDataSource(
            titles, icons,
            TAB_HOME
        )
        mBinding.bvBottom.setTextStyle(
            10,
            ResourcesUtils.getColor(R.color.C_414244),
            ResourcesUtils.getColor(R.color.C_5671FB)
        )
        mBinding.bvBottom.initDatas()
        mBinding.bvBottom.setOnItemOnclickListener { index -> selectedFragment(index) }
        selectedFragment(TAB_HOME)
    }

    private var mLastPosition = -1

    /**
     * 切换tab
     */
    private fun selectedFragment(index: Int) {
        //再次点击不处理
        if (mLastPosition == index) {
            return
        }
        val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
        val fragment = mFragmentList[index]
        if (mLastPosition != -1) {
            ft.hide(mFragmentList[mLastPosition])
        }
        if (fragment.isAdded) {
            ft.show(fragment)
        } else {
            ft.add(R.id.fl_container, fragment).show(fragment)
        }
        ft.commitAllowingStateLoss()
        mLastPosition = index
        mBinding.bvBottom.setSelectStyle(index)
    }

}